package com.gz.mylife.app;

import java.lang.ref.WeakReference;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.widget.OfflineDialog;
import com.zl.smartmall.library.IGZAddress;
import com.zl.smartmall.library.listener.AddHouseListener;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddHouseActivity extends BaseActivity {
	private EditText mEtCode;
	private Dialog mLoadingData;
	private OfflineDialog mOfflineDialog;
	private MenuItem saveItem;
	private TextView send_phone;

	private int cmid;
	private int id;
	private String phone;//获取验证码的手机号
	private MyAddHouseListener mListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addhouse);
		cmid = getIntent().getIntExtra("cmid", 0);
		id = getIntent().getIntExtra("id", 0);
		phone = getIntent().getStringExtra("phone");
		setActionbarTitle("提交验证码",false);
		mListener = new MyAddHouseListener(this);
		initView();
		
		String ss = phone.substring(0,phone.length()-(phone.substring(3)).length())+"****"+phone.substring(7);
		//send_phone.setText("验证码将发送至手机号码为"+ss+"的用户");
	}

	private void initView() {
		send_phone = (TextView)findViewById(R.id.send_phone);
		mLoadingData = new Dialog(this, R.style.DialogTheme);
		mLoadingData.setContentView(R.layout.loading_data_dialog2);
		mLoadingData.setCanceledOnTouchOutside(false);
		mOfflineDialog = (OfflineDialog) LayoutInflater.from(
				getApplicationContext()).inflate(R.layout.offline_dialog, null);
		mEtCode = (EditText) findViewById(R.id.et_smscode);
		mEtCode.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (saveItem != null) {
//					if (TextUtils.isEmpty(s.toString())) {
////						saveItem.setVisible(false);
//					} else {
//						saveItem.setVisible(true);
//					}
				}
			}
		});
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.updata_menu2, menu);
		saveItem = menu.findItem(R.id.action_updata);
//		saveItem = menu.add(0, 100, 0, "确定");
		MenuItemCompat.setShowAsAction(saveItem,MenuItemCompat.SHOW_AS_ACTION_ALWAYS|MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
//		if (TextUtils.isEmpty(mEtCode.getText().toString())) {
//			saveItem.setVisible(false);
//		}
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_updata:
			if(TextUtils.isEmpty(mEtCode.getText().toString())){
				Toast.makeText(this, "请先输入您的验证码", Toast.LENGTH_SHORT).show();
				return true;
			}
			IGZAddress.getInstance().addHouse(cmid, id,
					mEtCode.getText().toString(), mListener);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public static class MyAddHouseListener extends AddHouseListener {
		private AddHouseActivity mOuter;

		public MyAddHouseListener(AddHouseActivity outer) {
			this.mOuter = outer;
		}

		public void release() {
			new WeakReference<AddHouseActivity>(mOuter);
		}

		@Override
		public void onResponseSuccess() {
			if (mOuter == null) {
				return;
			}
			mOuter.mLoadingData.dismiss();
			mOuter.setResult(201);
			mOuter.finish();
		}

		@Override
		public void onSmsCodeError() {
			if (mOuter == null) {
				return;
			}
			mOuter.mLoadingData.dismiss();
			Toast.makeText(mOuter, "验证码错误", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onNoNetWork() {
			if (mOuter == null) {
				return;
			}
			mOuter.mLoadingData.dismiss();
			Toast.makeText(mOuter, Define.MSG_NO_NETWORK, Toast.LENGTH_SHORT)
					.show();
		}

		@Override
		public void onOffline() {
			if (mOuter == null) {
				return;
			}
			mOuter.mLoadingData.dismiss();
			mOuter.mOfflineDialog.show();
		}

		@Override
		public void onResponseFailed() {
			if (mOuter == null) {
				return;
			}
			mOuter.mLoadingData.dismiss();
			Toast.makeText(mOuter, "请求失败", Toast.LENGTH_SHORT).show();
		}

	}
}
